#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

int main(){
    int original_fd = open("test1.txt",O_WRONLY | O_CREAT,0644);
    if(original_fd == -1){
        perror("open");
        return 1;
    }
    int new_fd = dup(original_fd);
    if(new_fd == -1){
        perror("dup");
        close(original_fd);
        return 1;
    }
    const char *data = "Hello,world!\n";
    //通过原始文件描述符写入数据
    if(write(original_fd,data,strlen(data)) == -1){
        perror("write with original_fd");
        close(original_fd);
        close(new_fd);
        return 1;
    }
    //通过新文件描述符写入数据
    char additional_data[] = "This is additional data.\n";
    if(write(new_fd,additional_data,strlen(additional_data)) == -1){
        perror("write with new_fd");
        close(original_fd);
        close(new_fd);
        return 1;
    }
    close(original_fd);
    close(new_fd);
    return 0;
}